package com.adtech.adms.datemodel.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.adtech.adms.datemodel.pojo.DataModelObject;
import com.adtech.adms.device.pojo.GuiDatamodel;
import com.adtech.core.cache.Cache;
import com.adtech.core.dao.HibernateDAO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DataModelServiceImpl implements DataModelService {
	private HibernateDAO dao;
	private Cache cache;

	public HibernateDAO getDao() {
		return dao;
	}

	public void setDao(HibernateDAO dao) {
		this.dao = dao;
	}

	@Override
	public List<DataModelObject> getDataModelTree() {
		List<DataModelObject> dmos = new ArrayList<DataModelObject>();
		GuiDatamodel dataModel = dao.getOneByQuery(GuiDatamodel.class, null);
		String content = dataModel.getContent();
		ObjectMapper mapper = new ObjectMapper();
		try {
			JsonNode tree = mapper.readTree(content);
			JsonNode node = tree.get("root").get("node");
			for (JsonNode jsonNode : node) {
				dmos.add(mapper.convertValue(jsonNode, DataModelObject.class));
			}

		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
